package jpa.demo;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

	private static final SessionFactory sessionFactory;

	static {
		System.out.println("开始初始化Hibernate");
		AnnotationConfiguration config = new AnnotationConfiguration();
		System.out.println("配置Hibernate");
		config.setProperty("hibernate.connection.driver_class",
				"com.mysql.jdbc.Driver");
		config.setProperty("hibernate.connection.password", "admin");
		config.setProperty("hibernate.connection.url",
				"jdbc:mysql://localhost:3306/dev");
		config.setProperty("hibernate.connection.username", "root");
		config.setProperty("hibernate.dialect",
				"org.hibernate.dialect.MySQL5Dialect");
		config.setProperty("hbm2ddl.auto", "create");
		config.addAnnotatedClass(Company.class);
		System.out.println("完成配置");
		sessionFactory = config.buildSessionFactory();

	}

	public static Session getSession() throws HibernateException {
		return sessionFactory.openSession();
	}

}
